<template>
  <div id="app" class="container">
    <header class="header">
      <h1>会员签到界面设计</h1>
    </header>
    <div class="app">
      <div class="controls">
        <div style="flex: 1;">
          <div class="controls-header">
            <h3>设计元素基本配置</h3>
            <div class="header-decoration"></div>
          </div>
          <div class="device-controls">
            <div class="control-section">
              <h4 class="section-title">
                <span class="icon">🎨</span>
                颜色配置
              </h4>
              <div class="control-grid">
                <div class="control-item flex_row" v-for="item in colorList" :key="item.key">
                  <div class="control-label">{{ item.label }}</div>
                  <div class="control-input">
                    <PickerModel :showTitle="false" :mode="2" :color="`#${objectForm[item.key] || 'ffffff'}`"
                      @colorChange="colorChange(item.key, $event)" />
                  </div>
                </div>
              </div>
            </div>
            <div class="control-section">
              <h4 class="section-title">
                <span class="icon">🖼️</span>
                图标配置
              </h4>
              <div class="control-grid">
                <div class="control-item flex_row" v-for="item in imgBgList" :key="item.key">
                  <div class="control-label">{{ item.label }}</div>
                  <div class="control-input">
                    <UploadPhotos mWidth="85px" mHeight="85px" :list="objectForm[item.key]" :limit="1"
                      @update:list="updatePhotoQrCodeList(item.key, $event)" />
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="dialog-footer">
          <div class=""></div>
          <div class="action-buttons">
            <el-button type="danger" @click="submitHandle(1)" size="small" class="btn-clear">复位初始界面</el-button>
            <el-button type="primary" @click="submitHandle(2)" size="small" class="btn-confirm">保存配置</el-button>
          </div>
        </div>
      </div>
      <div class="simulator-container">
        <div class="simulator-header">
          <h3>预览效果</h3>
          <div class="header-decoration"></div>
        </div>
        <div class="mobile-simulator">
          <div class="device-wrapper">
            <div class="device-frame">
              <div class="notch"></div>
              <div class="device-screen">
                <div class="device-content">
                  <div class="signin-content">
                    <deviceMobileModel :initData="objectForm"></deviceMobileModel>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 收尾效果 -->
    <div class="footer-decoration">
      <div class="footer-white"></div>
      <div class="footer-blue"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch, getCurrentInstance, defineProps } from '@vue/composition-api'
import PickerModel from '@/components/commV2/pickerModel/index.vue'
import UploadPhotos from "@/components/commV2/UploadPhotos/index.vue"
import deviceMobileModel from "@/pages/signIn/components/deviceMobileModel.vue"

import config from "@/js/conf/Config.js"
// 获取当前组件实例
const { proxy } = getCurrentInstance()
const props = defineProps({
  isDisbableInfo: {
    type: Object,
    default: () => ({
      type: false,
    }),
  },
  detailID: {
    type: [String, Number],
    default: '',
  },
})

// 颜色配置列表
const colorList = ref([
  { key: 'font_c_tx', label: '签到提醒字体颜色' },
  { key: 'font_c_qd', label: '今日签到字体颜色' },
  { key: 'font_c_lq', label: '已领取字体颜色' },
  { key: 'btn_c_lq', label: '已领取按钮颜色' },
  { key: 'font_c_nlq', label: '未领取字体颜色' },
  { key: 'btn_c_nlq', label: '未领取按钮颜色' },
  { key: 'btn_share', label: '分享按钮颜色' },
  { key: 'font_c_share', label: '分享按钮字体颜色' },
  { key: 'line_c', label: '分割线颜色' },
  { key: 'back_c1', label: '下半部背景颜色' },
  { key: 'back_c2', label: '下半部渐变颜色' }
])

// 颜色配置列表
const imgBgList = ref([
  { key: 'img_lq', label: '已签到图标' },
  { key: 'img_nlq', label: '未签到图标' },
  { key: 'img_hd_bg', label: '头部背景图' },
  { key: 'img_hb', label: '红包图标' },
  { key: 'img_jf', label: '积分图标' },
  { key: 'img_day', label: '日历签到图标' },
])

const objectForm = ref({
  evenid: '', // 活动id 必填
  font_c_tx: 'ffffff', // 签到提醒字体颜色
  font_c_qd: 'ffffff', // 今日签到字体颜色
  font_c_lq: 'ffffff',// 已领取字体颜色  
  btn_c_lq: 'ffffff', // 已领取按钮颜色
  font_c_nlq: 'ffffff', // 未领取字体颜色
  btn_c_nlq: 'ffffff', // 未领取按钮颜色
  btn_share: 'ffffff', // 分享按钮颜色
  font_c_share: 'ffffff',// 分享按钮字体颜色
  line_c: 'ffffff', // 分割线颜色
  back_c1: 'ffffff', // 下半部背景颜色
  back_c2: 'ffffff', // 下半部渐变颜色(此值空 = 纯色;不等空=渐变)

  img_lq: [], // 已签到图标
  img_nlq: [], // 未签到图标
  img_hd_bg: [], // 头部背景图
  img_hb: [], // 红包图标
  img_jf: [], // 积分图标
  img_day: [], // 日历签到图标
  re_init: '',// 复位初始系统界面 1是 2否 默认否
})

const submitHandle = async (reInit) => {
  let param = {
    ...objectForm.value,
    img_lq: objectForm.value.img_lq[0].url,
    img_nlq: objectForm.value.img_nlq[0].url,
    img_hd_bg: objectForm.value.img_hd_bg[0].url,
    img_hb: objectForm.value.img_hb[0].url,
    img_jf: objectForm.value.img_jf[0].url,
    img_day: objectForm.value.img_day[0].url,
    re_init: reInit,
  }
  try {
    let res = await proxy.$api({
      url: config.yx_qd_tpl_save,
      data: param
    })
    if (res.status == 1) {
      proxy.$message({
        message: '保存成功',
        type: 'success',
      })
      getDetailInfo()
    } else {
      proxy.$message({
        message: res.msg,
        type: 'error',
      })
    }
  } catch (error) {
    proxy.$message({
      message: error.message,
      type: 'error',
    })
  }
}

// 先定义getDetailInfo函数
const getDetailInfo = async () => {
  let param = {
    evenid: props.detailID,
  }
  try {
    let res = await proxy.$api({
      url: config.yx_qd_tpl_info,
      data: param
    })
    if (res.status == 1) {
      objectForm.value = res.data?.tpl || {}
      objectForm.value.img_lq = [{ url: res.data?.tpl?.img_lq || '' }]
      objectForm.value.img_nlq = [{ url: res.data?.tpl?.img_nlq || '' }]
      objectForm.value.img_hd_bg = [{ url: res.data?.tpl?.img_hd_bg || '' }]
      objectForm.value.img_hb = [{ url: res.data?.tpl?.img_hb || '' }]
      objectForm.value.img_jf = [{ url: res.data?.tpl?.img_jf || '' }]
      objectForm.value.img_day = [{ url: res.data?.tpl?.img_day || '' }]
      updateInitData()
    }
  } catch (error) {

  }
}

// 然后再定义watch
watch(() => props.detailID, (newVal) => {
  if (newVal) {
    getDetailInfo()
  }
}, { deep: true, immediate: true })

const colorChange = (key, color) => {
  // 移除颜色值中的#前缀，只保存6位十六进制值
  if (color && typeof color === 'string') {
    objectForm.value[key] = color.replace('#', '')
    updateInitData()
  } else {
    objectForm.value[key] = 'ffffff' // 默认白色
  }
}

const updatePhotoQrCodeList = (key, newList) => {
  objectForm.value[key] = newList
}

const updateInitData = () => {
  objectForm.value = {
    ...objectForm.value,
    btn_c_lq: objectForm.value.btn_c_lq,
    font_c_lq: objectForm.value.font_c_lq,
    // 添加 btn_c_nlq 和 font_c_nlq 的更新
    btn_c_nlq: objectForm.value.btn_c_nlq,
    font_c_nlq: objectForm.value.font_c_nlq,
  };
};
</script>

<style scoped>
.container {
  width: 100%;
  margin: 0 auto;
  font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  height: 100vh;
  padding: 8px;
  color: #333;
  border-radius: 0;
  overflow: hidden;
  position: relative;
}

.header {
  text-align: center;
  color: #fff;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  margin-bottom: 6px;
}

.header h1 {
  font-size: 1.2rem;
  margin-bottom: 2px;
  font-weight: 700;
  background: linear-gradient(45deg, #fff, #f0f0f0);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  font-size: 0.7rem;
  opacity: 0.9;
  margin: 0;
  font-weight: 300;
}

.app {
  display: flex;
  gap: 8px;
  justify-content: center;
  max-width: 1400px;
  margin: 0 auto;
  height: calc(100vh - 80px);
}

.controls {
  flex: 1;
  max-width: 700px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  padding: 8px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.controls-header {
  margin-bottom: 4px;
  text-align: center;
}

.controls-header h3 {
  margin: 0 0 2px 0;
  color: #4a5568;
  font-size: 0.85rem;
  font-weight: 600;
  position: relative;
}

.header-decoration {
  width: 30px;
  height: 1px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  margin: 0 auto;
  border-radius: 1px;
}

.device-controls {
  flex: 1;
  overflow-y: auto;
  padding-right: 4px;
}

.device-controls::-webkit-scrollbar {
  width: 2px;
}

.device-controls::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.1);
  border-radius: 1px;
}

.device-controls::-webkit-scrollbar-thumb {
  background: linear-gradient(180deg, #667eea, #764ba2);
  border-radius: 1px;
}

.control-section {
  margin-bottom: 6px;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 3px;
  margin: 0 0 3px 0;
  color: #2d3748;
  font-size: 16px;
  font-weight: 600;
  padding-bottom: 1px;
  border-bottom: 1px solid #e2e8f0;
}

.icon {
  font-size: 0.7rem;
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
}

.control-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.control-item:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border-color: rgba(102, 126, 234, 0.3);
}

.control-label {
  font-weight: 500;
  color: #4a5568;
  width: 100%;
  margin-bottom: 1px;
  font-size: 14px;
  line-height: 1.0;
  text-align: left;
}

.control-input {}

.simulator-container {
  flex: 1;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  padding: 8px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  display: flex;
  flex-direction: column;
}

.simulator-header {
  margin-bottom: 4px;
  text-align: center;
}

.simulator-header h3 {
  margin: 0 0 2px 0;
  color: #4a5568;
  font-size: 0.85rem;
  font-weight: 600;
}

.mobile-simulator {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px 0;
}

.device-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  perspective: 1000px;
}

.device-frame {
  position: relative;
  width: 375px;
  height: 667px;
  transition: all 0.5s ease;
  box-shadow: 0 25px 80px rgba(0, 0, 0, 0.3);
  border-radius: 35px;
  background: linear-gradient(145deg, #1a1a1a, #2d2d2d);
  overflow: hidden;
  transform-style: preserve-3d;
  border: 6px solid #333;
}

.device-screen {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: white;
  border-radius: 29px;
}

.device-content {
  height: 100%;
  overflow-y: scroll;
  /* 改为scroll确保总是可滚动 */
  background: white;
  /* 隐藏滚动条但保持滚动功能 */
  scrollbar-width: none;
  /* Firefox */
  -ms-overflow-style: none;
  /* IE and Edge */
}

/* 隐藏WebKit浏览器的滚动条 */
.device-content::-webkit-scrollbar {
  display: none;
}

.signin-content {
  text-align: center;
}

.signin-content h2 {
  color: #667eea;
  margin-bottom: 8px;
  font-size: 16px;
  font-weight: 600;
}

.signin-content p {
  color: #666;
  font-size: 12px;
}

.notch {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100px;
  height: 20px;
  background: #1a1a1a;
  border-radius: 0 0 12px 12px;
  z-index: 10;
}

/* Responsive */
@media (max-width: 1200px) {
  .app {
    flex-direction: column;
    align-items: center;
    height: auto;
    gap: 10px;
  }

  .controls,
  .simulator-container {
    max-width: 100%;
    width: 100%;
  }

  .container {
    height: auto;
    min-height: 100vh;
  }
}

@media (max-width: 768px) {
  .container {
    padding: 6px;
    height: auto;
    min-height: 100vh;
  }

  .header h1 {
    font-size: 1.1rem;
  }

  .subtitle {
    font-size: 0.65rem;
  }

  .app {
    gap: 8px;
    height: auto;
  }

  .controls,
  .simulator-container {
    padding: 6px;
  }

  .control-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 4px;
  }

  .control-item {
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 4px 6px;
    min-height: 45px;
  }

  .control-label {
    margin-bottom: 2px;
    font-size: 14px;
  }

  .device-frame {
    width: 180px;
    height: 360px;
  }

  .section-title {
    font-size: 0.75rem;
  }

  .control-section {
    margin-bottom: 8px;
  }
}

@media (max-width: 480px) {
  .header h1 {
    font-size: 1rem;
  }

  .subtitle {
    font-size: 0.6rem;
  }

  .controls,
  .simulator-container {
    padding: 4px;
  }

  .control-grid {
    grid-template-columns: 1fr;
    gap: 3px;
  }

  .control-item {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 3px 4px;
    min-height: auto;
  }

  .control-label {
    margin-bottom: 0;
    margin-right: 6px;
    font-size: 14px;
    text-align: left;
  }

  .control-input {
    width: auto;
  }

  .device-frame {
    width: 160px;
    height: 320px;
  }
}

.dialog-footer {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 14px;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  border-radius: 8px;
  border: 1px solid #e2e8f0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);

  .action-buttons {
    display: flex;
    gap: 12px;
  }

  .btn-clear {
    background-color: #ef4444;
    border-color: #ef4444;
  }

  .btn-confirm {
    background-color: #3b82f6;
    border-color: #3b82f6;
  }
}
</style>